package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysRole;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysFamilyService;
import cn.mxb.logic.service.ISysRoleService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysUserAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/user/userList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/user/userAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/user/userUpdate.jsp";
	
	private SysUser sysUser;
	@Autowired
	private ISysUserService SysUserService;
	
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysFamilyService sysfamilyService;
	@Autowired
	private ISysRoleService sysRoleService;
	
	private Result<SysUser> pageResult;
	
	private List<SysUser> SysUserAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysRole> sysRoleAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	private Map<Integer,String> villlageNameMap = new HashMap<Integer, String>();  //村名
	
	private Map<String,String> familyNameMap = new HashMap<String, String>();
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysRoleAllList = sysRoleService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		SysUserService.add(sysUser);
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = SysUserService.find(sysUser, getPage(), getRow());
			for(SysUser sysUser : pageResult.getData()){
				if(null != sysUser.getCountId()  && !"".equals(sysUser.getCountId())){
					countNameMap.put(sysUser.getCountId(),sysCountService.findByProperty("countId", sysUser.getCountId()+"").getCountName());
				}
				if(null != sysUser.getTownId()  && !"".equals(sysUser.getTownId())){
					townNameMap.put(sysUser.getTownId(),sysTownService.findByProperty("sysTownId", sysUser.getTownId()+"").getSysTownName());
				}
				if(null != sysUser.getVillageId()  && !"".equals(sysUser.getVillageId())){
					villlageNameMap.put(sysUser.getVillageId(),sysVillageService.findByProperty("villageId", sysUser.getVillageId()+"").getVillageName());
				}
				if(null != sysUser.getFamilyId()  && !"".equals(sysUser.getFamilyId())){
					familyNameMap.put(sysUser.getFamilyId(),sysfamilyService.findByProperty("familyId", sysUser.getFamilyId()+"").getFamilyName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		SysUserService.delete(sysUser.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysUser =  SysUserService.findByProperty("id",sysUser.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		SysUserService.add(sysUser);
		sysUser = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysUser;
	}
	@Override
	public void prepare() throws Exception {
		if(sysUser ==null){
			sysUser = new SysUser();
		}
	}


	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}

	public Result<SysUser> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysUser> pageResult) {
		this.pageResult = pageResult;
	}

	
	public List<SysUser> getSysUserAllList() {
		return SysUserAllList;
	}

	public void setSysUserAllList(List<SysUser> SysUserAllList) {
		this.SysUserAllList = SysUserAllList;
	}

	

	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVilllageNameMap() {
		return villlageNameMap;
	}

	public void setVilllageNameMap(Map<Integer, String> villlageNameMap) {
		this.villlageNameMap = villlageNameMap;
	}

	public Map<String, String> getFamilyNameMap() {
		return familyNameMap;
	}

	public void setFamilyNameMap(Map<String, String> familyNameMap) {
		this.familyNameMap = familyNameMap;
	}

	public List<SysRole> getSysRoleAllList() {
		return sysRoleAllList;
	}

	public void setSysRoleAllList(List<SysRole> sysRoleAllList) {
		this.sysRoleAllList = sysRoleAllList;
	}

	

	
	

	
	
}
